from vec2d import Vec2d
import pygame

THRUSTER_IMAGE = "resources/thruster.png"

class Equipment:
    
    def __init__(self, mass = 0, power_required = 10):
        self.mass = mass
        self.power_required = power_required
        
    @property
    def mass(self):
        return self.mass
    
    @property
    def power_required(self):
        return self.power_required

class Thruster(Equipment):
    
    def __init__(self, mass, force):
        Equipment.__init__(self, mass)
        # Main thruster force
        self.main_thrust = Vec2d(0, 150000)
        # Side force
        self.side_thrust = Vec2d(100000, 0)
        
        self.main_level = 0
        self.side_level = 0
        
        self.image_sheet = pygame.image.load(THRUSTER_IMAGE)
        self.image_sheet = self.image_sheet.convert_alpha()
        self._image = [self.image_sheet.subsurface(pygame.Rect(0,0,10,10)),
                      self.image_sheet.subsurface(pygame.Rect(10,0,10,10)),
                      self.image_sheet.subsurface(pygame.Rect(20,0,10,10)),
                      self.image_sheet.subsurface(pygame.Rect(30,0,10,10))]

        
    def set_main_level(self, level = None, diff = None):
        if not level is None:
            self.main_level = level
        elif not diff is None:
            self.main_level += diff    
        
        if -1 > self.main_level: self.main_level = -1
        if 1 < self.main_level: self.main_level = 1
            
            
    def set_side_level(self, level = None, diff = None):
        if not level is None:
            self.side_level = level
        elif not diff is None:
            self.side_level += diff    
        
        if -1 > self.side_level: self.side_level = -1
        if 1 < self.side_level: self.side_level = 1 

    @property
    def image(self):
        if 0 <= self.main_level <= 0.1: return None
        elif self.main_level <= 0.3:    return self._image[0]
        elif self.main_level <= 0.7:    return self._image[1]
        else:                           return self._image[2]
        
    @property
    def side_level(self):
        return self.side_level
    
    @property
    def main_level(self):
        return self.main_level
    
    def set_thruster_level(self, main_level = None, side_level = None):
        if main_level is not None and -1 <= main_level <= 1:
            self.main_level = main_level
            
        if side_level is not None and -1 <= side_level <= 1:
            self.side_level = side_level
    
    def get_force(self, ship):
        return ((self.side_thrust * self.side_level) + \
                (self.main_thrust * self.main_level)) / ship.total_mass()
                
        
        
        
""" A weightless generator. Thats because its just hypothetical.
"""
class Generator(Equipment):
    
    def __init__(self, mass = 0, power_output = 0):
        Equipment.__init__(self, mass, 0)
        self.power_output = power_output
        
    @property
    def power_output(self):
        return self.power_output
    
class BasicGenerator(Generator):
    
    def __init__(self):
        Generator.__init__(self, mass = 5000, power_output = 50)
        
    